import axios from 'axios'

const state = {
	start: 0,
	hotMovies: []
}

const mutations = {
	getHotMovies ( state, payload ) {
		for (var i = payload.res.length - 1; i >= 0; i--) {
			state.hotMovies.push(payload.res[i]);
		}
	},
	refresh ( state ) {
		state.start = 0;
		state.hotMovies = []
	}
}

const actions = {
	getHotMovies(context) {
		axios.get('/dbapi/movie/in_theaters', {
			params: {
				start: context.state.start,
				count: 5
			}
		})
		.then(function (res) {
			context.state.start += 5;
			context.commit({
				type: 'getHotMovies',
				res: res.data.subjects
			})
		})
		.catch(function( err ) {
			console.log(err)
		})
	}
}

export default {
	state,
	mutations,
	actions
}